package net.wisos.opn.service;

import net.wisos.opn.cms.dao.ChannelDao;
import net.wisos.opn.cms.entity.News;
import net.wisos.opn.cms.entity.Channel;
import net.wisos.opn.dao.ProductDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;


@Component
public class ProductManager {
	@Autowired
	private ProductDao productDao;
	@Autowired
	private ChannelDao categoryDao;

	public News getProduct(Long productId) {
		return productDao.findOne(productId);
	}

	public void saveProduct(News product) {
		productDao.save(product);
	}

	public void deleteProduct(Long productId) {
		productDao.delete(productId);
	}

	/**
	 * 按分类列出产品.
	 */
	public Page<News> findProductsByCategory(Long categoryId) {
		Channel category = categoryDao.findOne(categoryId);
		return productDao.findByCategory(category, null);
	}

	/**
	 * 按分类id获得分类.
	 */
	public Channel getCategory(Long categoryId) {
		return categoryDao.findOne(categoryId);
	}

	public void saveCategory(Channel category) {
		categoryDao.save(category);
	}

	public void deleteCategory(Long categoryId) {
		categoryDao.delete(categoryId);
	}

	/**
	 * 获取全部分类.
	 */
	public Iterable<Channel> getCategories() {
		return categoryDao.findAll();
	}

}
